<script setup lang="ts">
import useDialog from '@/hooks/useDialog';
import { adjustWebSocketUrl } from '@/utils/adjustWebSocketUrl';
import { useAssistStore } from '@/store/modules/Assist/useAssistStore';
import { previewURLs } from '@/api/video';

const { surroundingVideoData: treeData } = storeToRefs(useAssistStore());
const { title, visible, closeDialog, openDialog } = useDialog({
  title: '视频监控'
});
const videoUrl = ref('');
const videoComManagementRef = ref(null);
const playVideoHandler = (value) => {
  getUrl(value);
};
const videoCode = ref('');
const getUrl = async (item) => {
  videoCode.value = item.code;
  const res = await previewURLs({
    cameraIndexCode: item.code,
    protocol: 'ws'
  });
  videoUrl.value = res.data;

  // 视频切换后，延迟调用播放器的resize方法
  nextTick(() => {
    setTimeout(() => {
      if (videoComManagementRef.value && videoComManagementRef.value.resizePlayer) {
        videoComManagementRef.value.resizePlayer();
      }
    }, 500);
  });
};
defineExpose({
  open: openDialog,
  close: closeDialog
});

watch(visible, (val) => {
  if (val) {
    getUrl(treeData.value[0]);
  }
});
</script>

<template>
  <el-dialog
    :title="title"
    :model-value="visible"
    :draggable="true"
    width="40%"
    :before-close="closeDialog"
    append-to-body
    class="largeScreen-dialog assistant-dialog"
  >
    <div class="main-box">
      <div class="main-box-left">
        <el-scrollbar height="250">
          <div class="station-module-main">
            <div class="station-item" v-for="(item, index) in treeData" :key="index" @click="playVideoHandler(item)" v-show="treeData.length > 0">
              <div :title="item.name">{{ item.name }}</div>
              <div class="station-item-left"></div>
            </div>
            <div class="station-item-left" v-if="treeData.length == 0" style="color: #1aecff; font-size: 19px; font-weight: 700">暂无数据</div>
          </div>
        </el-scrollbar>
      </div>
      <div class="main-box-right">
        <video-com-management
          id="assistantVideo"
          :camera-index-code="videoCode"
          video-type="0"
          camera-type="0"
          :value="adjustWebSocketUrl(videoUrl)"
          ref="videoComManagementRef"
          v-if="visible"
        />
      </div>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">
.main-box {
  padding: 0 20px;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .main-box-left {
    width: 40%;
    height: 95%;

    .station-item {
      color: #00ffff;
      position: relative;
      width: 100%;
      min-height: 20px;
      background-color: #012536;
      margin-top: 10px;
      line-height: 30px;
      padding-left: 20px;
      box-sizing: border-box;
      font-size: 14px;
      cursor: pointer;

      :nth-child(1) {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .value-label {
        padding-right: 10px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
      }

      .station-item-left {
        position: absolute;
        width: 4px;
        height: 100%;
        background-color: #43ff8b;
        left: 0;
        top: 0;
      }
    }
  }

  .main-box-right {
    width: 400px;
    height: 300px;
  }
}
</style>
<style>
.assistant-dialog .el-dialog__body {
  background-color: #083577;
}
</style>
